package com.lqd.demo.springboot;

import java.util.List;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.lqd.demo.springboot.config.bean.IndexFilter;
import com.lqd.demo.springboot.config.bean.IndexListener;
import com.lqd.demo.springboot.config.bean.IndexServlet;

//@SpringBootApplication() = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan
/*@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan*/
//@SpringBootApplication(exclude = MybatisAutoConfiguration.class)
@SpringBootApplication
@ServletComponentScan
@MapperScan("com.lqd.demo.springboot.orm.mybatis")
public class App extends WebMvcConfigurerAdapter{

	public static void main(String[] args) {
		 
		SpringApplication.run(App.class, args) ;
	}
	
	/**
	 * -------------jack 转 fastjson start --------------
	 */
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		
		super.configureMessageConverters(converters);
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		FastJsonConfig fastConfig = new FastJsonConfig();
		fastConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		fastConverter.setFastJsonConfig(fastConfig);
		converters.add(fastConverter);
	}
	
/*	@Bean
	public HttpMessageConverters fastJsonConverters(){
		
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		FastJsonConfig fastConfig = new FastJsonConfig();
		fastConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		HttpMessageConverter<?> converter = fastConverter ;
		return new HttpMessageConverters(converter);
	}*/
	
	/**
	 * -------------jack 转 fastjson end --------------
	 */
	
	/**
	 * -------------filter listener servlet- bean\\\start --------
	 */
	@Bean
	public FilterRegistrationBean indexFilter()
	{
		FilterRegistrationBean filter = new FilterRegistrationBean(new IndexFilter());
		filter.addUrlPatterns("/*");
		return filter;
	}
	
	@Bean
	public ServletRegistrationBean indexServlet()
	{
		ServletRegistrationBean servlet = new ServletRegistrationBean(new IndexServlet());
		servlet.addUrlMappings("*.do");
		return servlet;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Bean
	public ServletListenerRegistrationBean indexListener()
	{
		ServletListenerRegistrationBean listener = new ServletListenerRegistrationBean(new IndexListener());
		return listener;
	}
	/**
	 * -------------filter listener servlet-\\\end --------
	 */
	
	/**
	 * -------------tomcat 容器配置 -\\\start ----------------
	 */
	/*@Bean
	public EmbeddedServletContainerFactory servletContainer()
	{
		TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
		factory.setPort(8888);
		factory.setUriEncoding(Charset.defaultCharset());
		return factory;
	}
	
	*/
	
	@Bean
	public EmbeddedServletContainerFactory servletContainerJetty()
	{
		JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory();
		factory.setPort(8777);
		return factory;
	}
	
	/**
	 * -------------tomcat 容器配置 -\\\end ----------------
	 */
}
